package com.shaoshaohuo.app.ui.ec;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.fragment.CarFragment;
import com.shaoshaohuo.app.ui.fragment.EcCartFragment;
import com.shaoshaohuo.app.ui.fragment.EcMainFragment;
import com.shaoshaohuo.app.ui.fragment.EcMainFragmentWithBottonNavigation;
import com.shaoshaohuo.app.ui.fragment.UserFragment;

public class MyFragmentActivity extends BaseActivity {

	private int mFragmentId = 1;

	private UserFragment mUserFragment;
	private CarFragment mCarFragment;
	private Fragment mEcFragment;

	private Fragment mContent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_tab_layout);

		initData();
		initView();
		setUpView();
	}

	private void initData() {
		mFragmentId = getIntent().getIntExtra(ExtraName.id, 1);
	}

	private void initView() {
		
	}


	private void setUpView() {
		switch (mFragmentId) {
		case 1:
			if (mUserFragment == null) {
				String title = getIntent().getStringExtra(UserFragment.ARGUMENT);
				mUserFragment = UserFragment.newInstance(title);
			}
			switchContent(mContent, mUserFragment);
			break;
		case 2:
			if (mCarFragment == null) {
				String title = getIntent().getStringExtra(CarFragment.ARGUMENT);
				mCarFragment = CarFragment.newInstance(title);
			}
			switchContent(mContent, mCarFragment);
			break;
		case 3:
			if (mEcFragment == null) {
				String title = getIntent().getStringExtra(EcMainFragment.ARGUMENT);
//				mEcFragment = EcMainFragment.newInstance(title);
				mEcFragment = new EcMainFragmentWithBottonNavigation();
			}
			switchContent(mContent, mEcFragment);
			break;
		}
	}

	private void switchContent(Fragment from, Fragment to) {
		if (mContent != null) {
			if (to != mContent) {
				FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
				if (!to.isAdded()) { // 先判断是否被add过
					if (to.getClass().getName().equals(from.getClass().getName())) {
						transaction.remove(from).add(R.id.layout_content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
					} else {
						transaction.hide(from).add(R.id.layout_content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
					}
				} else {
					transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
				}
				mContent = to;
			}
		} else {
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.layout_content, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
			mContent = to;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent result) {
		super.onActivityResult(requestCode, resultCode, result);
		
		//处理电商购物车fragment回调通知
		Fragment f = getSupportFragmentManager().findFragmentByTag(EcCartFragment.class.getSimpleName());
        /*然后在碎片中调用重写的onActivityResult方法*/
		if (f != null) {
			f.onActivityResult(requestCode, resultCode, result);
		}
	}

	
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
//				getSupportFragmentManager().popBackStack();
//			}
//		}
//		return super.onKeyDown(keyCode, event);
//	}

}
